package com.jqb.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpServletContext extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/*网站访问计数器*/
	private int count;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext context = this.getServletContext();
		/*读取应用目录下的文件*/
		InputStream is = context.getResourceAsStream("/WEB-INF/jdbc.properties");
		/*src路径下可以用ClassLoad读取*/
//		InputStream is = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		Properties prop = new Properties();
		prop.load(is);
		String driverClass = prop.getProperty("driverClass");
		/*获取文件的物理路径*/
		String realPath = context.getRealPath("jdbc.properties");
		System.out.println("driverClass="+driverClass);
		System.out.println("realPath="+realPath);
		is.close();
		
		count++;
		/*ServletContext是在服务器端共享的域*/
		/*为ServletContext域设置*/
		context.setAttribute("counter", count);
		/*从ServletContext中取值*/
		String counter = String.valueOf(context.getAttribute("counter"));
		System.out.println("counter="+counter);
	}
}
